package br.pucrs.lognplay.faces.bean;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import br.pucrs.lognplay.entity.Client;
import br.pucrs.lognplay.entity.LogSource;
import br.pucrs.lognplay.entity.User.Role;
import br.pucrs.lognplay.service.LogSourceManagementService;

@ManagedBean
@RequestScoped
public class LogSourceListBean extends Bean {

	private List<LogSource> logSources;

	@EJB
	private LogSourceManagementService logSourceManagementService;

	public List<LogSource> getLogSources() {
		try {
			if (logSources == null) {
				Role loggedUserRole = getLoggedUser().getRole();

				if (loggedUserRole.equals(Role.MASTER_USER)) {
					logSources = logSourceManagementService.listAllLogSources();
				} else if (loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
					Client loggedUserClient = getLoggedUser().getClient();
					logSources = logSourceManagementService.listLogSourcesByClientId(loggedUserClient.getId());
				}
			}

			return logSources;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}
}
